using System.Configuration;
using System.Diagnostics.CodeAnalysis;

namespace TfsSpam.Differ.Configuration
{
    /// <summary>
    /// The FileTypes class represents the fileTypes element in the TFS Spam
    /// configuration section and contains the file types that should have
    /// detailed information included in the checkin notification e-mails.
    /// </summary>
    public class FileTypes : ConfigurationSection
    {
        #region -- Public properties
        /// <summary>
        /// The collection of <see cref="FileType"/> objects that represent
        /// the file types that should have detailed information included.
        /// </summary>
        [ConfigurationProperty( "", IsDefaultCollection = true )]
        public FileTypeCollection Types
        {
            get { return (FileTypeCollection) base[ "" ]; }
        }
        #endregion
    }

    /// <summary>
    /// The FileType class represents a single file type that
    /// should have detail information included.
    /// </summary>
    public class FileType : ConfigurationElement
    {
        #region -- Public properties
        /// <summary>
        /// The extension (including the period) of a file type that should
        /// have detail information included.
        /// </summary>
        [ConfigurationProperty( "extension", IsRequired = true, IsKey = true )]
        public string Extension
        {
            get { return (string) base[ "extension" ]; }
        }
        #endregion
    }

    /// <summary>
    /// The FileTypeCollection class represents a collection of fileType elements
    /// in the configuration file and defines the list of file types that should
    /// have detail information included in the checkin notification e-mails.
    /// </summary>
    [SuppressMessage( "Microsoft.Design", "CA1010", Justification = "This is the standard way of implementing a configuration element collection" )]
    [ConfigurationCollection( typeof( FileType ), AddItemName = "fileType", CollectionType = ConfigurationElementCollectionType.BasicMap )]
    public class FileTypeCollection : ConfigurationElementCollection
    {
        #region -- Base class overrides
        /// <summary>
        /// Creates a new <see cref="FileType"/> object to be included
        /// in the collection.
        /// </summary>
        /// <returns>A new object to include in the collection.</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new FileType();
        }

        /// <summary>
        /// Retrieves the key for an element in the collection.
        /// </summary>
        /// <param name="element">The element whose key should be retrieved.</param>
        /// <returns>The key for the specified element.</returns>
        protected override object GetElementKey( ConfigurationElement element )
        {
            return ( (FileType) element ).Extension;
        }
        #endregion
    }
}
